package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorServiceImpl;

/**
 * Servlet implementation class UpdateVisitorServlet.
 */
@WebServlet("/UpdateVisitorServlet")
public class UpdateVisitorServlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
       
    /**
     * Instantiates a new update visitor servlet.
     *
     * @see HttpServlet#HttpServlet()
     */
    public UpdateVisitorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		RequestDispatcher rd=request.getRequestDispatcher("updateVisitor.jsp");  
        rd.forward(request,response);  
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
            VisitorServiceImpl vis = new VisitorServiceImpl();
			HttpSession session = request.getSession(true);
			String username = request.getParameter("username");
			String name = request.getParameter("name");
			String lastname = request.getParameter("lastname");
			String age = request.getParameter("age").toString();
			String address = request.getParameter("address");
			String phone = request.getParameter("phone");
			
            Visitor visitor = vis.searchVisitorById((Integer)(session.getAttribute("VisitorID")));                               
            
            if (!username.equals(visitor.getUsername())){
            	visitor.setUsername(request.getParameter("username"));
            }
            if (!name.equals(visitor.getName())){
            	visitor.setName(request.getParameter("name"));
            }
            if (!lastname.equals(visitor.getLastName())){
            	visitor.setLastName(request.getParameter("lastname"));
            }
            if (!age.equals(visitor.getAge())){
            	visitor.setAge(Integer.parseInt(request.getParameter("age").replace("/", "")));
            }
            if (!address.equals(visitor.getAddress())){
            	visitor.setAddress(request.getParameter("address").replace("/", ""));
            }
            if (!phone.equals(visitor.getPhoneNumber())){
            	visitor.setPhoneNumber(request.getParameter("phone"));
            }

            vis.updateVisitor(visitor);  
            
            visitor = vis.searchVisitorByUsername(visitor.getUsername());
            Cookie visitorIDCookie = new Cookie("visitorID",String.valueOf(visitor.getVisitorId()));
            Cookie loginCookie = new Cookie("visitor",String.valueOf(visitor.getUsername()));
            
            response.addCookie(loginCookie);
            response.addCookie(visitorIDCookie);
            session.setAttribute("Session", 2);
            
            if(visitor.isAdmin()){
       		 	response.sendRedirect("listEventAdmin.jsp");
       	 	}else{
       	 		response.sendRedirect("listEventVisitor.jsp");
       	 	}
        }catch(Exception theException){
             System.out.println(theException); 
        }
    }
}
